﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using MaxStudio.Concrete.Web;
using MaxStudio.Concrete.DBUtility;
using MaxStudio.Concrete.Server;
using MaxStudio.Concrete.Server.Service;
using MaxStudio.Concrete.Server.Dao;
using MaxStudio.Concrete.Component;

namespace MaxStudio.Concrete.Web.Pages
{
    public partial class Motorcade : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
               BindGridView();
               WebPartBind.BindMasterDataDDL(this.ddlStatus,Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDMotorcadeStatus"].ToString()));
            }
        }

        #region Private Method 

        private void BindGridView()
        {
            MotorcadeService motorcadeServiceObj = ServiceFactory.getMotorcadeService();
            OperationResult<List<tb_Motorcade>> result = motorcadeServiceObj.GetAllMotorcade();

            if (result.operationSuccess)
            {
                gvRMList.DataSource = result.obj;
                gvRMList.DataBind();
            }
            else
            {
                //EXCEPTION
                //return null;
            }
        }

        private void BindEdieDialog(int id)
        { 
           WebPartBind.BindMasterDataDDL(this.ddlEditStatus,   Int32.Parse(System.Configuration.ConfigurationSettings.AppSettings["MDMotorcadeStatus"].ToString()));

            MotorcadeService motorcadeService = ServiceFactory.getMotorcadeService();

            OperationResult<tb_Motorcade> resultEdit = motorcadeService.GetMotorcadeById(id);
            if (resultEdit.operationSuccess == true)
            {

                txtEditCarNo.Text = resultEdit.obj.CarNo;
                txtEditDescription.Text = resultEdit.obj.Description;
                txtEditDriver.Text = resultEdit.obj.Driver;
                txtEditLoading.Text =  resultEdit.obj.Loading;
                txtEditName.Text =  resultEdit.obj.Name;
                ddlEditStatus.SelectedValue = resultEdit.obj.Status.ToString(); 

                lbLastModier.Text = "TEST";
                lbEidtId.Text = resultEdit.obj.Id.ToString();
                lbLastModifyDate.Text = resultEdit.obj.ModifyDate.ToShortDateString();
            }
        }

        private void DeleteRecords(int id)
        {
            lbDeleteId.Text = id.ToString();
        }

        public string GetMasterDataNameById(string strTypeId)
        {
            int masterDataId = Int32.Parse(strTypeId);
            return MasterDateString.GetMasterDataNameById(masterDataId);
        } 
        #endregion

        #region Event
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            MotorcadeService MotorcadeServiceObj = ServiceFactory.getMotorcadeService();
            tb_Motorcade instance = new tb_Motorcade();

            instance.CarNo = txtCarNo.Text;
            instance.Description = txtDescription.Text;
            instance.Driver = txtDriver.Text;
            instance.Loading = txtLoading.Text;
            instance.Name = txtName.Text;
            instance.Status = Int32.Parse(ddlStatus.SelectedValue);

            OperationResult<tb_Motorcade> result = MotorcadeServiceObj.CreateMotorcade(instance);
            BindGridView(); 
        }
        protected void btnEdit_Click(object sender, EventArgs e)
        {
            int editId = 0;
            if (lbEidtId.Text.Length > 0)
            {
                editId = Int32.Parse(lbEidtId.Text);
            }

            MotorcadeService MotorcadeServiceObj = ServiceFactory.getMotorcadeService();
            tb_Motorcade entity = new tb_Motorcade();

            entity.Id = editId;

            entity.CarNo = txtEditCarNo.Text;
            entity.Description = txtEditDescription.Text;
            entity.Driver = txtEditDriver.Text;
            entity.Loading = txtEditLoading.Text;
            entity.Name = txtEditName.Text;
            entity.Status = Int32.Parse(ddlEditStatus.SelectedValue);  
            entity.ModifyDate = DateTime.Now;
            entity.ModifyBy = 1;
            entity.CreateDate = DateTime.Now;

            OperationResult<tb_Motorcade> resultEdit = MotorcadeServiceObj.ModifyMotorcade(entity);
            BindGridView();
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            int deleteId = Int32.Parse(lbDeleteId.Text);
            MotorcadeService MotorcadeServiceObj = ServiceFactory.getMotorcadeService();
            tb_Motorcade deleteEntity = new tb_Motorcade();
            deleteEntity.Id = deleteId;
            MotorcadeServiceObj.DeleteMotorcade(deleteEntity);
            BindGridView();
        }

        protected void gvRMList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Select")
            {
                string strSelectId = e.CommandArgument.ToString();
                BindEdieDialog(Int32.Parse(strSelectId));
            }

            if (e.CommandName == "del")
            {
                string strSelectId = e.CommandArgument.ToString();
                DeleteRecords(Int32.Parse(strSelectId));
            }
        }

        protected void gvRMList_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvRMList.PageIndex = e.NewPageIndex;
            BindGridView();
        }
        #endregion
    }
}
